package com.zonski.jbots.game.entity;

import com.zonski.jbots.engine.entity.AbstractMind;
import com.zonski.jbots.engine.entity.MovingEntity;
import com.zonski.jbots.engine.entity.Mind;
import com.zonski.jbots.engine.*;
import com.zonski.jbots.game.EntityHelper;
import com.zonski.jbots.game.EntityFilter;
import com.zonski.jbots.game.JBotsConstants;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 14, 2003
 * Time: 10:30:05 AM
 * To change this template use Options | File Templates.
 */
public class LaserMind extends AbstractMind implements EntityFilter
{
    public LaserMind()
    {
    }

    public void update(Engine engine)
    {
        // adjust the size of the laser to fit the free space
        int tileSize = engine.getRoom().getTileSize();
        Rectangle bounds = this.getEntity().bounds;
        int tx;
        int ty;
        int direction = this.getEntity().direction;
        ((MovingEntity)this.getEntity()).setTargetDirection(direction);
        switch(direction)
        {
            default:
            case Directions.SOUTH:
                tx = bounds.getCentreX() / tileSize;
                ty = bounds.bottomEdge / tileSize;
                break;
            case Directions.NORTH:
                tx = bounds.getCentreX() / tileSize;
                ty = bounds.y / tileSize;
                break;
            case Directions.WEST:
                tx = bounds.rightEdge / tileSize;
                ty = bounds.getCentreY() / tileSize;
                break;
            case Directions.EAST:
                tx = bounds.x / tileSize;
                ty = bounds.getCentreY() / tileSize;
                break;
        }
        int tilesAcross = engine.getRoom().getNumberOfTilesAcross();
        int tilesDown = engine.getRoom().getNumberOfTilesDown();
        // collides with the side of the room
        switch(direction)
        {
            default:
            case Directions.SOUTH:
                bounds.setY(0, bounds.bottomEdge);
                break;
            case Directions.NORTH:
                bounds.setHeight(tilesDown * tileSize - bounds.y);
                break;
            case Directions.WEST:
                bounds.setX(0, bounds.rightEdge);
                break;
            case Directions.EAST:
                bounds.setWidth(tilesAcross * tileSize - bounds.x);
                break;
        }
        QuickVector entities = EntityHelper.getInterestingEntities(this, tx, ty, direction, engine);
        if(entities != null && entities.size() > 0)
        {
            // should only have one entity in it
            Entity found = (Entity)entities.elementAt(0);
            Rectangle collisionBounds = ((Entity)entities.elementAt(0)).bounds;
            switch(direction)
            {
                default:
                case Directions.SOUTH:
                    bounds.setY(collisionBounds.bottomEdge - 2,
                            bounds.bottomEdge - collisionBounds.bottomEdge + 2);
                    break;
                case Directions.NORTH:
                    bounds.setHeight(collisionBounds.y - bounds.y + 2);
                    break;
                case Directions.WEST:
                    bounds.setX(collisionBounds.rightEdge - 2,
                            bounds.rightEdge - collisionBounds.rightEdge + 2);
                    break;
                case Directions.EAST:
                    bounds.setWidth(collisionBounds.x - bounds.x + 2);
                    break;
            }
        }
    }

    // from Entity Filter

    public final boolean isInteresting(Entity entity)
    {
        return entity.entityType.getId() != JBotsConstants.LASER_ID &&
                entity.entityType.getId() != JBotsConstants.PAIN_ID &&
                entity != this.getEntity() &&
                entity.bounds.overlaps(this.getEntity().bounds);
    }

    public final boolean isStopper(Entity entity)
    {
        return isInteresting(entity);
    }

    public Mind copy()
    {
        return new LaserMind();
    }
}
